import { ProColumns } from "@ant-design/pro-table/es/typing";
import DigitRange from "@/components/DigitRange";
import React from "react";
import { Select, SelectProps } from "antd";
import enumeration from "common/utils/enumeration";

export function getDateTimeWithRangeSearch({
  title,
  dataIndex,
  hideInTable,
}: {
  title: string,
  dataIndex: string;
  hideInTable?: boolean;
}): ProColumns[]{
  return [
    {
      title: title,
      valueType: 'dateTime',
      dataIndex: dataIndex,
      search: false,
      hideInTable,
    },
    {
      title: title,
      valueType: 'dateTimeRange',
      key: `${dataIndex}_search`,
      fieldProps: {
        allowEmpty: [true, true],
      },
      hideInTable: true,
      search: {
        transform: (value: string[])=>{
          return {
            [`${dataIndex}Start`]: value?.[0],
            [`${dataIndex}End`]: value?.[1],
          }
        }
      },
    },
  ]
}

export function getMoneyWithRangeSearch({
                                                     title,
                                                     dataIndex,
                                                   }: {
  title: string,
  dataIndex: string;
}): ProColumns[]{
  return [
    {
      title,
      valueType: 'money',
      dataIndex,
      renderFormItem: ()=>{
        return <DigitRange placeholders={['最少金额', '最多金额']} />
      },
      search: {
        transform: (value: number[])=>{
          return {
            [`${dataIndex}Start`]: value?.[0],
            [`${dataIndex}End`]: value?.[1],
          }
        }
      }
    }
  ]
}

export function getEnumWithSearch(props: {
  title: string,
  dataIndex: string;
  enum?: ReturnType<typeof enumeration>;
  options?: SelectProps['options'];
}): ProColumns[]{
  const {
    title,
    dataIndex,
    options,
  } = props;
  return [
    {
      title,
      key: dataIndex,
      render: (_, record: any)=>{
        return options?.find(item=>item.value === record[dataIndex])?.label ?? props.enum?.[record[dataIndex] as keyof typeof props.enum];
      },
      renderFormItem: ()=>{
        return <Select
          options={options as any ?? props.enum?.$listSelect}
          allowClear
          placeholder="全部"
        />
      },
    }
  ]
}
